//
//  main.swift
//  week_1
//
//  Created by MoriW on 2017/9/13.
//  Copyright © 2017年 MoriW. All rights reserved.
//

import Foundation

struct student{
    var name : String
    var mach : Double
    var english : Double
    var avg : Double

    init(name : String , mach : Double , english : Double) {
        self.name = name
        self.mach = mach
        self.english = english
        self.avg = Double(self.mach + self.english)/2
    
    }
}

func count(students:[student]) -> [student]{
    var result = students
    for i in 0...result.count-2 {
        for j in i+1...result.count-1{
            if(result[i].avg > result[j].avg){
                let tmp = result[i]
                result[i] = result[j]
                result[j] = tmp
            }
        }
    }
       return result
}

var student01 = student(name: "w1", mach: 67, english: 60)
var student02 = student(name: "w2", mach: 50, english: 98)
var student03 = student(name: "w3", mach: 83, english: 80)
var student04 = student(name: "w4", mach: 85, english: 97)
var student05 = student(name: "w5", mach: 75, english: 88)
var student06 = student(name: "w6", mach: 69, english: 58)
var student07 = student(name: "w7", mach: 97, english: 66)
var student08 = student(name: "w8", mach: 77, english: 94)
var student09 = student(name: "w9", mach: 64, english: 78)
var student10 = student(name: "w10", mach: 87, english: 60)


var result = count(students: [student01,student02,student03,student04,student05,student06,student07,student08,student09,student10])

print("从小到大为:")
//遍历结果数组并打印
for i in 0..<result.count{
    print("姓名:\(result[i].name),数学:\(result[i].mach),英语:\(result[i].english),平均分:\(result[i].avg)")
}
